<?php
class Student extends MY_Controller
{
	var $profile;
	var $p_id;
	public function __construct()
	{
		parent::__construct();
		$this->page_auth->secure_page();
		$this->load->library('hs');
		$this->load->helper('grade');
		$this->view_data['system_message'] = $this->_msg();
	}
	
	public function index(){ }
	
	/**
	 * Student subjects and grades per school year
	 */
	public function student_grades()
	{
		$this->load->model('M_gperiods');
		$this->load->model('M_enrollees');
		$this->load->model('M_student_grades','stud_grades');
		$this->load->model('M_profiles');
		$this->load->model('M_grade_smiley');

		$id = $this->session->userdata('id_e');
		$sp = $this->M_enrollees->get_enrollee_prof($id); if(!$sp){ show_404(); }
		$pid = $sp->profile_id;
		
		$this->view_data['grading_periods'] = $this->view_data['gp'] = $this->M_gperiods->get_gperiods();
		$this->view_data['student_all_profiles'] = $this->M_profiles->get_student_full_profile($pid);
		$this->view_data['id'] = $id;
		$this->view_data['enrollment_id'] = $id;
		$this->view_data['gd'] = $this->stud_grades->get_grading_system();
		$this->view_data['gs'] = $gs = $this->M_grade_smiley->get_grade_smiley();
	}

	public function profile()
	{
		if($this->session->userdata('id_e'))
		{
			$id = $this->session->userdata('id_e');
			$this->load->model('M_sections');
			$this->load->model('M_student');
			$this->load->model('M_enrollees');
			$this->load->model('M_student_subjects');
			$this->load->model('M_student_grades');
			$this->load->model('M_gperiods');
			$this->load->model('M_levels');
			$this->view_data['student'] = $student = $this->M_student->get_student($id);
			$this->view_data['level_dropdown'] = $this->M_levels->get_all_levels_array();
			$this->view_data['student'] = $this->M_student->get_student($id);
			$this->view_data['sections'] = $this->M_sections->get_section_by_grade_level_array($this->view_data['student']->level_id);
			$this->view_data['enrollment'] = $this->M_enrollees->get_section_by_profile($id);
			$this->view_data['grading_periods'] = $this->M_gperiods->get_gperiods();
			$this->view_data['student_subjects'] = $this->M_student_subjects->get_all_by_enrollment_id($id);
			$this->view_data['student_grades'] = $student_grades = $this->M_student_grades->grouped_subject_by_enrollment_id($id);
			$this->view_data['imastudent'] = TRUE;
			
		}else{
		
			
		}
	}
	
	
	public function registration()
	{
		$this->load->model('M_school_settings','mss');
		$this->view_data['registration'] = $this->mss->check_registration();
		$this->view_data['reg_data'] = $y = $this->mss->get_next_school_year();
		
		if($this->input->post('register_for_schoolyear'))
		{
			$x = $this->hs->encrypt(intval($this->input->post('school_year_id')),intval($y->sy_yr_from),intval($y->sy_yr_to));
			if($x)
			{
				$this->_msg('s','Please Confirm your registration','registration/register/'.$x);
				
				print_r($x);
				die();
			}else{
				$this->_msg('e','An Unexpected Error has occured, Please refresh this page and try again.','student/registration');
			}
		}
	}
	
	function change_password()
	{
		$this->view_data['system_message'] = $this->_msg();
		if($this->input->post())
		{
			$this->load->model('M_users');
			$this->load->library('form_validation');
			$old_pass 	= $this->input->post('old_password');
			$new_pass 	= $this->input->post('new_password');
			$c_new_pass = $this->input->post('c_new_password');
			
			if($this->form_validation->run('user_change_pass') == FALSE)
			{
				$this->view_data['system_message'] = validation_errors() == '' ? NULL : $this->_msg('e',validation_errors());
			}else
			{	
				$id = $this->session->userdata('id_e');
				$this->load->model('M_student');
				$enroll = $this->M_student->get_student($id);
				
				$result = $this->M_users->change_pass($old_pass,$new_pass,$enroll->user_id);
				
				if($result->stat == TRUE)
				{
					$this->_send_change_password_to_email($enroll,'student',$new_pass); //send to email
					$this->_msg('p','<div class="alert alert-success">Successfully Changed Your password, please login with new credentials</div>','auth/logout');
				}else{
					$this->_msg('e',$result->log,'student/change_password');
				}
			}
		}
	}

  private function _send_change_password_to_email($val, $usertype, $newpassword)
  {
    $this->load->library('email');
		$this->email->initialize(array( 
		   'mailtype' => 'html', 
		));

		$usertype = $usertype == "parent" ? "guardian" : $usertype;
		$school_name = ucwords($this->school_name);
		$school_name_title = $this->_accronymizer($this->school_name).'@DONOTREPLY.com';
		
		if($this->school_tag_name == FALSE)
		{
			$tagname = 'part of '.$school_name;
		}else{
			$tagname = 'a '.strtoupper($this->school_tag_name);
		}
		
		$this->load->model('M_enrollees');

		$data['school_name'] = $school_name;
		$data['acc'] = $school_name_title;
		$data['link'] = $this->student_link;
		$data['tagname'] = $tagname;
		$data['password'] = $newpassword;
		$data['dob'] = date('m-d-Y',strtotime($val->dob));
		
		$data['name'] = $val->lastname.','.$val->firstname.' '.$val->middlename;
		if($val->name_ext){
			$data['name'] .= ' '.$val->name_ext;
		}

		$message = $this->load->view('email_templates/student_change_password',$data,TRUE);

		$this->email->from($school_name_title,$school_name);
		$this->email->to($val->email);
		$this->email->subject('Login Details Updated.');
		$this->email->message($message);
		$this->email->send();	
		
		return true;
  }

/* ========================================= */
	function grades()
	{
		$id = $this->session->userdata('id_e');
		$this->load->model('M_student_grades');
		$this->load->model('M_gperiods');
		$this->load->model('M_enrollees');
		$this->load->model('M_student');
		$this->load->model('M_student_grades','stud_grades');
		
		// $this->view_data['student'] = $enrollment = $this->M_student->get_student($id);
		
		// $this->view_data['enrollment'] = $this->M_enrollees->get_section_by_profile($this->view_data['student']->profile_id);
		// $this->view_data['grading_periods'] = $this->M_gperiods->get_gperiods();
		// $this->view_data['student_grades'] = $student_grades = $this->M_student_grades->grouped_subject_by_enrollment_id($enrollment->enrollment_id);
		
		// if(!(empty($student_grades)))
		// {
		// 	$this->view_data['grades'] = $this->_loop_throught_grades($student_grades, $enrollment->enrollment_id);
		// }

		// vd($student_grades);

		$this->load->model('M_student_grades');
		$this->load->model('M_gperiods');
		$this->load->model('M_enrollees');
		$this->load->model('M_student');
		$this->view_data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($id);
		$this->view_data['student'] = $this->M_student->get_student($id);
		$this->view_data['enrollment']= $this->M_enrollees->get_section_by_profile($id);
		$this->view_data['grading_periods'] = $this->M_gperiods->get_gperiods();
		$this->view_data['student_grades'] = $student_grades = $this->M_student_grades->grouped_subject_by_enrollment_id($id);
		$this->view_data['gd'] = $this->stud_grades->get_grading_system();
		// vp($this->db->last_query());
		// vd($student_grades);

		// $this->student_links();
	
		if(!(empty($student_grades)))
		{
			$this->view_data['grades'] = $this->_loop_throught_grades($student_grades, $id);
		}
		// vd($this->view_data['grades']);
	}
	
	private function _loop_throught_grades($student_grades, $enrollment_id)
	{
		foreach($student_grades as $row)
		{
		  $result = $this->M_student_grades->get_all_by_enrollment_id($enrollment_id);
		}
		return $result;
	}
	
	
/* ========================================= */
	function subjects()
	{
		$id = $this->session->userdata('id_e');
		$this->load->model('M_student_grades');
		$this->load->model('M_gperiods');
		$this->load->model('M_levels');
		$this->load->model('M_enrollees');
		
		$this->load->model('M_student');
		$this->load->model('M_student_subjects');
		$this->view_data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($id);
		$this->view_data['student'] = $p = $this->M_student->get_student($id);
		$this->view_data['enrollment']= $this->M_enrollees->get_section_by_profile($id);
		$this->view_data['student_subjects'] = $this->M_student_subjects->get_all_schedule_by_student_subjects($id);
	}
/* ========================================= */
	
	public function fees($e_id='')
	{
		$e_id = $this->session->userdata('id_e');
		if(!is_numeric($e_id))
		{
			show_error('Invalid Student',404);
		}else{
			$this->load->helper('misc');
			$this->load->model('M_enrollees','me');
			$this->load->model('M_student_payment_record','mspr');
			$this->load->model('M_student_payment_breakdown','mspb');
			
			$this->view_data['payment_mode'] = $this->me->get_student_payment_mode($e_id);
			// $this->view_data['student_deductions'] = $this->M_scholarships->get_student_deductions($e_id);
			$this->view_data['total_stud_deductions'] = ''/*$this->M_scholarships->sum_all_student_deductions($e_id)*/;
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			
			$this->view_data['tuit_fees'] = $this->me->get_student_fees($e_id,$this->current_school_year,'tuit');
			$this->view_data['misc_fees'] = $this->me->get_student_fees($e_id,$this->current_school_year,'misc');
			$this->view_data['cca_fees'] = $this->me->get_student_fees($e_id,$this->current_school_year,'cca');
			$this->view_data['elec_fees'] = $this->me->get_student_fees($e_id,$this->current_school_year,'elec');
			
			// $this->view_data['modal']['scholarships'] = $this->M_scholarships->get_scholarships_available_for_stud($e_id);
			// $this->view_data['modal']['id'] = $e_id;
			
			$this->view_data['enrollee_data'] = $this->me->get_enrollee_prof($e_id);
			$this->view_data['sum_paid_fees'] = $this->me->get_sum_of_paid_fees($e_id,$this->current_school_year);
			$this->view_data['sum_amount_due'] = $this->me->get_sum_of_amount_due($e_id,$this->current_school_year);
			$this->view_data['profile_id'] = NULL;
			$this->view_data['enrollment_id'] = $e_id;
			$this->view_data['level_id'] = $this->view_data['enrollee_data']->level_id;
			
			$payment_data['amount_due'] = $this->view_data['sum_amount_due'];
			$payment_data['amount_paid'] = $this->view_data['sum_paid_fees'];
			
			if((!empty($payment_data['amount_due']) AND !empty($payment_data['amount_paid'])) AND ($payment_data['amount_due'] - $payment_data['amount_paid']) == 0)
			{
				$this->M_student_payment_record->update_fully_paid_status($e_id);
				$this->view_data['finance_status'] = $this->_msg('n','Student Is Already Full Paid');
			}
			
			$this->session->set_userdata('payment_data','');
			$this->session->set_userdata('payment_data',(object)$payment_data);
			
			$c_spr_id = $this->mspr->current_payment_record_id($e_id);

			if($c_spr_id)
			{
				$this->load->model('M_student_payment_breakdown','mspb');
				$electives = $this->mspb->get_specific_payment_breakdown_by_spr_id($c_spr_id,'elec');
				$type='spb';
			}else{
				$this->load->model('M_enrollees','me');
			    $electives = $this->me->get_student_fees($e_id,'','elec');
				$type='sef';
			}
			
			
			//FOR STUDENT OVER THE COUNTER PAYMENTS
			$this->view_data['otcp'] = $this->me->get_otcp($e_id);
			$this->view_data['payment_modes'] = $this->me->get_student_current_payment_mode_and_due_by_enrollment_id($e_id);
			$this->view_data['others'] = $electives;
			
		}
	}

	public function commendations()
	{

		$this->load->library('pagination');
		$this->load->model('M_student','ms');
		$e_id = $this->session->userdata('id_e');
		
		$config['base_url'] = base_url('student/commendations');
		$config['per_page'] = 10;
		$config['uri_segment'] = 3;

		$config['full_tag_open'] = '<div><ul class="pagination">';
		$config['full_tag_close'] = '</ul></div>';

		$config['first_link'] = 'First';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = '&gt;';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&lt;';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="current"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';

		$config['total_rows'] = $this->ms->count_stud_commendations($e_id);
		
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
		
		$this->view_data['commendations'] = $this->ms->get_stud_commendations($e_id,$page,$config['per_page']);
		$this->view_data['pagnation_links'] = $this->pagination->create_links();
	}
}

?>
